import javax.vecmath.*;
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import java.io.*;

public class tcpipLayer {
    void sendData(Client connectionToPlayer, String data) throws IOException {
//        System.out.println(data);
        connectionToPlayer.sendData(data);
        //Client temp = new Client();
        //temp.setServerToConnectTo("127.0.0.1");
        //temp.run();
        //temp.sendData(data);
        //temp.closeClient();
    }

    void getData(Server tcpServer, Vector3d data, Vector3d backupData, String objectName) {
        //read buffer string////////////////////////////////////

    	//if(objectName=="puck"){
    	//	System.out.println("reading puck");
    	//}
        //parse the xml and set values
        
        String dataFromOtherPlayer=tcpServer.passData().trim();
        if (dataFromOtherPlayer=="") {
            data=null;
            return;
        }
        //System.out.println(dataFromOtherPlayer);
        //STATIC DATA!!!
        //dataFromOtherPlayer="<data><puck><x>1</x><y>2</y><z>-13</z></puck></data>";
        try {
            DocumentBuilderFactory dbf =
            DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(dataFromOtherPlayer));
            Document doc = db.parse(is);    
            NodeList nodes = doc.getElementsByTagName(objectName);

            Element element = (Element) nodes.item(0);
            NodeList xnodes = element.getElementsByTagName("x");
            Element xnode = (Element) xnodes.item(0);
            NodeList xvalues = xnode.getChildNodes();
            String xvalue = ((Node) xvalues.item(0)).getNodeValue();
            

            NodeList ynodes = element.getElementsByTagName("y");
            Element ynode = (Element) ynodes.item(0);
            NodeList yvalues = ynode.getChildNodes();
            String yvalue = ((Node) yvalues.item(0)).getNodeValue();
            

            NodeList znodes = element.getElementsByTagName("z");
            Element znode = (Element) znodes.item(0);
            NodeList zvalues = znode.getChildNodes();
            String zvalue = ((Node) zvalues.item(0)).getNodeValue();

            /*
            System.out.println("Cords for "+objectName+": ");
            System.out.println("x: " + xvalue);
            System.out.println("y: " + yvalue);
            System.out.println("z: " + zvalue);
            //  xvalue.trim().to
            */

            float xvalueFloat = Float.parseFloat(xvalue);
            float yvalueFloat = Float.parseFloat(yvalue);
            float zvalueFloat = Float.parseFloat(zvalue);

            data.set(Double.parseDouble(xvalue),Double.parseDouble(yvalue),Double.parseDouble(zvalue));
            //data.setX(xvalueFloat);
            //data.setY(yvalueFloat);
            //data.setZ(zvalueFloat);

        } catch (Exception e) {
            //e.printStackTrace();
            data.set(backupData.x,backupData.y, backupData.z);
        }
        //System.out.println(dataFromOtherPlayer);
    }
    void getVelocityData(Server tcpServer, Vector3f data, Vector3f backupData, String objectName) {
        //read buffer string////////////////////////////////////

    	//if(objectName=="puck"){
    	//	System.out.println("reading puck");
    	//}
        //parse the xml and set values
        
        String dataFromOtherPlayer=tcpServer.passData().trim();
        if (dataFromOtherPlayer=="") {
            data=null;
            return;
        }
        
        //STATIC DATA!!!
        //dataFromOtherPlayer="<data><puck><x>1</x><y>2</y><z>-13</z></puck></data>";
        try {
            DocumentBuilderFactory dbf =
            DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(dataFromOtherPlayer));
            Document doc = db.parse(is);    
            NodeList nodes = doc.getElementsByTagName(objectName);

            Element element = (Element) nodes.item(0);
            NodeList xnodes = element.getElementsByTagName("x");
            Element xnode = (Element) xnodes.item(0);
            NodeList xvalues = xnode.getChildNodes();
            String xvalue = ((Node) xvalues.item(0)).getNodeValue();
            

            NodeList ynodes = element.getElementsByTagName("y");
            Element ynode = (Element) ynodes.item(0);
            NodeList yvalues = ynode.getChildNodes();
            String yvalue = ((Node) yvalues.item(0)).getNodeValue();
            

            NodeList znodes = element.getElementsByTagName("z");
            Element znode = (Element) znodes.item(0);
            NodeList zvalues = znode.getChildNodes();
            String zvalue = ((Node) zvalues.item(0)).getNodeValue();

            /*
            System.out.println("Cords for "+objectName+": ");
            System.out.println("x: " + xvalue);
            System.out.println("y: " + yvalue);
            System.out.println("z: " + zvalue);
            //  xvalue.trim().to
            */

            float xvalueFloat = Float.parseFloat(xvalue);
            float yvalueFloat = Float.parseFloat(yvalue);
            float zvalueFloat = Float.parseFloat(zvalue);

            data.set(xvalueFloat,yvalueFloat,zvalueFloat);
            //data.setX(xvalueFloat);
            //data.setY(yvalueFloat);
            //data.setZ(zvalueFloat);

        } catch (Exception e) {
            //e.printStackTrace();
            data.set(backupData.x,backupData.y, backupData.z);
        }
        //System.out.println(dataFromOtherPlayer);
    }
}
